{% extends "blog/index.html" %}

{% block title %}
搜索{{ query }} - 残阳似血的技术博客
{% endblock %}

{% block css %}
<link type="text/css" rel="stylesheet" href="/static/blog/css/SyntaxHighlighter/shCoreDefault.css"><link> 
{% endblock %}

{% block js %}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 
<script language="JavaScript" src="/static/blog/js/jquery.pagination.js"></script> 
<script language="JavaScript"> 

	var enable_load = false;
	$(function(){
		$("div#footer-bottom p.bottom-right a:last").click(function(){
			targetOffset = $("body").offset().top;
			$('html,body').stop().animate({scrollTop: targetOffset}, 500);
			return false;
		});
		
		$("#Pagination").pagination("{{ count }}",{
			callback:pageselectCallback,
			prev_text: '&laquo 上一页',
			next_text: '下一页 &raquo;',
			items_per_page: 10,
			num_display_entries:6,
			current_page: '{{ page }}',
			num_edge_entries: 2,
			link_to: '#main',
			prev_show_always:false,
			next_show_always:false
		});
	})
	
	function pageselectCallback(page_id, jq) { 
		initData(page_id); 
	} 
	
	function initData(pageIndex){
		if(enable_load){
			$.ajax({
				type: "GET",
			    url: "/search/?q={{ query }}&page=" + (Number(pageIndex)+1) + "&from=ajax",
			    success: function(msg){
					$("div#container").empty();
			        $(msg).prependTo("div#container");
					
					var index = new Number(pageIndex);
					var n_page = 10;
					var count = new Number("{{ count }}");
					var start = index * n_page + 1;
					var end = count > (index + 1) * n_page ? 
					    (index + 1) * n_page : count;
					var show = start == end ? start : start + " - " + end;
					var msg = "共搜索到{{ count }}条结果，以下是" + show + "条";
					
					$("#resultCount").text(msg);
			    }
			});
		}
		else{
			enable_load = true;
		}
		
		$('body').ajaxStart(function(){
			$(this).addClass('overlay')
		});
		$('body').ajaxComplete(function(){
			$(this).removeClass('overlay')
		});
	}
	
	
	
</script> 
{% endblock %}

{% block content %}
{% if results %}
    <div class="post">
		<p id="resultCount" class="highlight">共搜索到{{ count }}条结果，以下是{% if start == end %}{{ start }}{% else %}{{ start }} - {{ end }}{% endif %}条</p> 
	</div>
	<div id="container">
    {% for item in results %}
	    <div class="post">
	    	<h4><a href="{{ item.link }}" style="color:#147DB6;">{{ item.title }}</a></h4>
			<p>{{ item.htmlSnippet|safe }}</p>
			<p><a href="{{ item.link }}">{{ item.link }}</a></p>
		</div>
	{% endfor %}
	</div>
	{% if count > 10 %}
	    <div id="Pagination" class="pagination" style="padding: 20px 0 20px 30px;"></div>
	{% endif %}
{% else %}
<div class="post">
	<p class="highlight">搜索" {{ query }} "没有任何结果</p> 
</div>
{% endif %}
{% endblock %}